package com.zhengdongzulin.zuke.app.work.activity

import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.text.SpannableString
import android.text.Spanned
import android.text.TextUtils
import android.text.style.ForegroundColorSpan
import android.view.View
import android.widget.ImageView
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lxj.androidktx.core.getVM
import com.lxj.androidktx.core.toast
import com.lxj.androidktx.core.vertical
import com.zhengdongzulin.zuke.R
import com.zhengdongzulin.zuke.app.data.WorkListDetailBean
import com.zhengdongzulin.zuke.app.work.vm.ZKWorkListDetailVm
import com.zhengdongzulin.zuke.base.BaseActivity
import com.zhengdongzulin.zuke.databinding.DialogLifeServiceAppraiseBinding
import com.zhengdongzulin.zuke.databinding.ZkActivityWorkListDetailBinding
import com.zhengdongzulin.zuke.ext.getBottomDialog
import com.zhengdongzulin.zuke.ext.loadImage

/**
 * @类名称: CLASS
 * @类描述:
 * @创建人：Jeff
 * @创建时间：2021/6/9 20:53
 * @备注：
 */
class ZKWorkListDetailActivity :
    BaseActivity<ZKWorkListDetailVm, ZkActivityWorkListDetailBinding>() {

    override fun getLayoutResId(): Int = R.layout.zk_activity_work_list_detail

    override fun initVM(): ZKWorkListDetailVm = getVM(ZKWorkListDetailVm::class.java)

    override fun initView() {
        setTitleContent("工单详情")
    }

    override fun initData() {
        mView.tvOk.visibility = View.GONE
        idIntent = intent.getStringExtra("id")
        mViewModel.getWorkDetailById(idIntent!!)
    }

    override fun initListener() {
        mView.tvOk.setOnClickListener {
            appraise()
        }
    }

    override fun startObserve() {
        mViewModel.getWorkDetail.observe(this, Observer {

            processId = it.processId
            mView.tvType.text = "${it.incidentType}(${it.title})"
            mView.tvContent.text = it.description
            mView.tvStatus.text = it.status
            mView.tvUserName.text = "申请人：${it.linkmanName}(${it.linkmanPhone})"
            mView.tvUserTime.text = "创建时间：${it.lastModifiedDate}"

            when (it.incidentTypeImage) {
                "1" -> {
                    mView.ivType.setImageResource(R.drawable.zk_icon_work_list_type_baoxiu)
                }
                "2" -> {
                    mView.ivType.setImageResource(R.drawable.zk_icon_work_list_type_baoshi)
                }
                "3" -> {
                    mView.ivType.setImageResource(R.drawable.zk_icon_work_list_type_fuwu)
                }
            }
            when (it.status) {
                "待分派" -> {
                    mView.tvStatus.setBackgroundDrawable(GradientDrawable().apply {
                        setColor(Color.parseColor("#EF6D65"))
                        cornerRadius = 8.0f
                    })
                }
                "待接单" -> {
                    mView.tvStatus
                        .setBackgroundDrawable(GradientDrawable().apply {
                            setColor(Color.parseColor("#EF6D65"))
                            cornerRadius = 8.0f
                        })
                }
                "待签到" -> {
                    mView.tvStatus
                        .setBackgroundDrawable(GradientDrawable().apply {
                            setColor(Color.parseColor("#FF9900"))
                            cornerRadius = 8.0f
                        })
                }
                "处理中" -> {
                    mView.tvStatus
                        .setBackgroundDrawable(GradientDrawable().apply {
                            setColor(Color.parseColor("#FF9900"))
                            cornerRadius = 5.0f
                        })
                }
                "待审核" -> {
                    mView.tvStatus
                        .setBackgroundDrawable(GradientDrawable().apply {
                            setColor(Color.parseColor("#00AB77"))
                            cornerRadius = 8.0f
                        })
                }
                "待验收" -> {
                    mView.tvStatus
                        .setBackgroundDrawable(GradientDrawable().apply {
                            setColor(Color.parseColor("#1273F7"))
                            cornerRadius = 8.0f
                        })

                    mView.tvOk.visibility = View.VISIBLE
                }
                "已完成" -> {
                    mView.tvStatus
                        .setBackgroundDrawable(GradientDrawable().apply {
                            setColor(Color.parseColor("#00AB77"))
                            cornerRadius = 8.0f
                        })
                }
            }

            mView.recyclerViewPic.apply {
                layoutManager = GridLayoutManager(this@ZKWorkListDetailActivity, 4)
                it?.imgArr?.also {
                    adapter = VPicAdpater(it).apply {
                        setClick(object : Click {
                            override fun choose() {

                            }
                        })
                    }
                }
            }

            mAdapter = VProcessListAdpater(it.gongDanProcessInfoList)
            mView.recyclerView.vertical().adapter = mAdapter

        })
        mViewModel.getWorkDetail.state.observe(this, getObserver(mViewModel.getWorkDetail))

        mViewModel.end_gong_dan.observe(this, Observer {
            initData()
        })
        mViewModel.end_gong_dan.state.observe(this, getObserver(mViewModel.end_gong_dan))
    }

    private var processId: String = ""
    private var idIntent: String? = ""
    var mData: MutableList<WorkListDetailBean.ProcessInfoListBean> = mutableListOf()
    lateinit var mAdapter: VProcessListAdpater

    inner class VProcessListAdpater(
        list: MutableList<WorkListDetailBean.ProcessInfoListBean>
    ) : BaseQuickAdapter<WorkListDetailBean.ProcessInfoListBean, BaseViewHolder>(
        R.layout.zk_item_work_detail_process_list,
        list
    ) {
        override fun convert(holder: BaseViewHolder, item: WorkListDetailBean.ProcessInfoListBean) {

            if (holder.position == 0) {
                holder.getView<View>(R.id.v_top).visibility = View.INVISIBLE
            } else {
                holder.getView<View>(R.id.v_top).visibility = View.VISIBLE
            }

            holder.setText(R.id.tv_user_time, item.date)

            if (TextUtils.isEmpty(item.name)) {
                holder.setText(R.id.tv_content, item.description)
            } else {
                var span = SpannableString("${item.description} ${item.name} ")
                span.setSpan(
                    ForegroundColorSpan(Color.parseColor("#1273f7")),
                    0,
                    item.description.length,
                    Spanned.SPAN_EXCLUSIVE_INCLUSIVE
                )
                holder.setText(R.id.tv_content, span)
            }
        }
    }

    /**
     * 评价订单
     */
    fun appraise() {
        var view = DialogLifeServiceAppraiseBinding.inflate(layoutInflater)
        var mDialog = this.getBottomDialog(view.root, 0.0)
        view.ivClose.setOnClickListener {
            mDialog.dismiss()
        }
        //非常满意(FCMY)、满意(MY)、一般(YB)、差(C)、非常差(FCC)
        view.tvAppraiseDesc.text = "很不满意\n(1分)"
        view.tvAppraiseDesc.tag = "FCC"

        var ratingBar = view.ratingBar.apply {
            rating = 1.0f
            setOnRatingBarChangeListener { ratingBar, rating, fromUser ->
                var grade = ratingBar.rating
                when (grade) {
                    1.0f -> {
                        view.tvAppraiseDesc.text = "很不满意\n(1分)"
                        view.tvAppraiseDesc.tag = "FCC"
                    }
                    2.0f -> {
                        view.tvAppraiseDesc.text = "不满意\n(2分)"
                        view.tvAppraiseDesc.tag = "C"
                    }
                    3.0f -> {
                        view.tvAppraiseDesc.text = "一般\n(3分)"
                        view.tvAppraiseDesc.tag = "YB"
                    }
                    4.0f -> {
                        view.tvAppraiseDesc.text = "满意\n(4分)"
                        view.tvAppraiseDesc.tag = "MY"
                    }
                    5.0f -> {
                        view.tvAppraiseDesc.text = "很满意\n(5分)"
                        view.tvAppraiseDesc.tag = "FCMY"
                    }
                    else -> {
                        view.tvAppraiseDesc.text = "您的评价会让工作人员做的更好！"
                        view.tvAppraiseDesc.tag = ""
                    }
                }
            }
        }
        view.ok.text = "确认验收"
        view.ok.setOnClickListener {
            var grade = ratingBar.rating
            var gradeContent = view.etAppraiseInput.text.trim().toString()
            if (grade == 0.0f) {
                toast("请选择评分")
                return@setOnClickListener
            }
            if (gradeContent.isNullOrEmpty()) {
                toast("请输入服务内容")
                return@setOnClickListener
            }
            mViewModel.end_gong_dan(
                idIntent!!,
                processId,
                gradeContent,
                checkLevel = view.tvAppraiseDesc.tag.toString()
            )
            mDialog.dismiss()
        }
        mDialog.show()
    }


    interface Click {
        fun choose()
    }

    inner class VPicAdpater(
        list: MutableList<String>
    ) : BaseQuickAdapter<String, BaseViewHolder>(
        R.layout.zk_item_add_work_choose_pic,
        list
    ) {
        var mclick: Click? = null

        fun setClick(click: Click) {
            this.mclick = click
        }

        override fun convert(holder: BaseViewHolder, item: String) {
            holder.loadImage(
                R.id.photo,
                item,
                R.mipmap.error_empty_zheng,
                6.0f
            )
            holder.getView<ImageView>(R.id.delete).visibility = View.GONE
            holder.getView<ImageView>(R.id.photo).setOnClickListener {
                mclick?.choose()
            }
        }
    }

}